﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenGameKit;

namespace TestApp
{
    class Program
    {
        static Scene MyScene = new Scene(640, 480);
        static Sprite MySprite;
        static Sprite MySprite2;
        static int MyTexture;
        static TileSheet MySheet;

        static void Main(string[] args)
        {
            Game.CreateWindow(640, 480, "OpenGameKit Test App");
            Game.OnStart += Game_OnStart;
            Game.OnUpdate += Game_OnUpdate;
            Game.OnRender += Game_OnRender;
            Game.OnEnd += Game_OnEnd;

            Game.Run();
        }

        static void Game_OnStart(object sender, GameEventArgs e)
        {
            Game.CurrentScene = MyScene;

            MyTexture = Graphics.LoadImage("Resources/test.png", Color.White);

            MySprite = new Sprite("Resources/test.png", 100, 100, 1, Color.White);
            MySprite.CollisionMode = Sprite.CollisionType.Circle;

            MyScene.AddSprite(MySprite);

            MySprite2 = new Sprite("Resources/test2.png", 300, 200, 0, Color.White);

            MyScene.AddSprite(MySprite2);

            MySheet = new TileSheet("Resources/test.png", 16, 16, Color.White);
            MySheet.Define(1, 1, 0);

            MyScene.CurrentTileSheet = MySheet;
            MyScene.SetTile(3, 3, 1);
            MyScene.BackColor = Color.Black;

            MyScene.RunScript("Resources/mapFile.js");
            Graphics.CreateFont(0, "Resources/arial.ttf", 12);
        }

        static void Game_OnUpdate(object sender, GameEventArgs e)
        {
            MySprite.ImageAngle = GameMath.LookAt(MySprite.X, MySprite.Y, 0, 0);
            MySprite.Speed = 2;

            if (Game.GetKeyDown() == Game.Key.Q)
                Game.Exit();

            if (Game.GetMouseDown() == Game.MouseKey.Left)
            {
                MySprite.X = MyScene.GetMouseSceneX();
                MySprite.Y = MyScene.GetMouseSceneY();
            }

            MyScene.FocusView((int)MySprite.X, (int)MySprite.Y);
        }

        static void Game_OnRender(object sender, GameEventArgs e)
        {
            Graphics.DrawImage(MyTexture, 16, 16, 32, 32);

            Graphics.SetFillColor(Color.Green);
            Graphics.DrawCircle(200, 200, 10);

            if (MySprite.CheckCollision(MySprite2, Sprite.CollisionType.Rectangle) == true)
            {
                Graphics.SetFillColor(Color.Red);
                Graphics.DrawRectangle(0, 0, 64, 64);
            }

            Graphics.DrawText(0, "Hello World!", 0, 0, Color.Green);
        }

        static void Game_OnEnd(object sender, GameEventArgs e)
        {
            Graphics.DisposeImage(MyTexture);
            MySprite.Dispose();
            MySprite2.Dispose();
        }
    }
}
